cmake_minimum_required(VERSION 3.10)

project(Template)

# Find package FreeRTOS
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/system/freertos)
find_package(FreeRTOS)

file(GLOB SRCS
    ${PROJECT_SOURCE_DIR}/app/*.cpp
    ${PROJECT_SOURCE_DIR}/system/*.cpp
    ${PROJECT_SOURCE_DIR}/system/std_peripheral/src/*.c

    ${PROJECT_SOURCE_DIR}/driver/usart/*.cpp
    ${PROJECT_SOURCE_DIR}/driver/iic/*.cpp
    ${PROJECT_SOURCE_DIR}/driver/adc/*.cpp

    ${PROJECT_SOURCE_DIR}/app/breathing_light/*.cpp
    ${PROJECT_SOURCE_DIR}/app/fan_control/*.cpp
    ${PROJECT_SOURCE_DIR}/app/UV_control/*.cpp
    ${PROJECT_SOURCE_DIR}/app/L2CDTP/*.cpp
    ${PROJECT_SOURCE_DIR}/app/SpindleMotor/*.cpp
    ${PROJECT_SOURCE_DIR}/app/CompressorMotor/*.cpp
    ${PROJECT_SOURCE_DIR}/app/CRC/*.c
    ${PROJECT_SOURCE_DIR}/app/L2Dataset/*.cpp
    ${PROJECT_SOURCE_DIR}/app/DataMaintenance/*.cpp
    ${PROJECT_SOURCE_DIR}/app/ScreenComm/*.cpp
)

include_directories(
    ${FreeRTOS_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/app
    ${PROJECT_SOURCE_DIR}/system
    ${PROJECT_SOURCE_DIR}/system/std_peripheral/inc
    ${PROJECT_SOURCE_DIR}/system/platform


    ${PROJECT_SOURCE_DIR}/driver/usart
    ${PROJECT_SOURCE_DIR}/driver/iic
    ${PROJECT_SOURCE_DIR}/driver/adc

    ${PROJECT_SOURCE_DIR}/app/breathing_light
    ${PROJECT_SOURCE_DIR}/app/fan_control
    ${PROJECT_SOURCE_DIR}/app/UV_control
    ${PROJECT_SOURCE_DIR}/app/L2CDTP
    ${PROJECT_SOURCE_DIR}/app/SpindleMotor
    ${PROJECT_SOURCE_DIR}/app/CompressorMotor
    ${PROJECT_SOURCE_DIR}/app/CRC
    ${PROJECT_SOURCE_DIR}/app/L2Dataset
    ${PROJECT_SOURCE_DIR}/app/DataMaintenance
    ${PROJECT_SOURCE_DIR}/app/ScreenComm
)

set(CMAKE_BUILD_TYPE debug)

set(STARTUP_FILE ${PROJECT_SOURCE_DIR}/startup.s)

set(LINKER_SCRIPT ${PROJECT_SOURCE_DIR}/link.ld)

set(LIBS "-lm -lstdc++ -lc")

enable_language(ASM)

set(CROSS_COMPILER_PREFIX /home/jiayi/Tools/arm/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi/bin/)

set(CMAKE_C_COMPILER ${CROSS_COMPILER_PREFIX}arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER_PREFIX}arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILER_PREFIX}arm-none-eabi-gcc)
set(CMAKE_OBJCOPY ${CROSS_COMPILER_PREFIX}arm-none-eabi-objcopy)
set(CMAKE_SIZE ${CROSS_COMPILER_PREFIX}arm-none-eabi-size)

set(MCU_FLAGS "-mcpu=cortex-m0plus -mthumb")

set(COMPILE_MARCOS "-DAPM32F030xC")

set(CMAKE_ASM_FLAGS "${COMPILE_MARCOS} ${MCU_FLAGS} -O0 -Wall -fdata-sections -ffunction-sections")
set(CMAKE_C_FLAGS "${COMPILE_MARCOS} ${MCU_FLAGS} -O0 -Wall -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${COMPILE_MARCOS} ${MCU_FLAGS} -O0  -Wall -fdata-sections -ffunction-sections")

set(CMAKE_EXE_LINKER_FLAGS "${MCU_FLAGS} -T ${LINKER_SCRIPT} -O0 -specs=nano.specs -specs=nosys.specs ${LIBS} -Wl,-Map=${PROJECT_NAME}.map,--cref -Wl,--gc-sections -Wl,-u_printf_float")

add_executable(${PROJECT_NAME}.elf ${STARTUP_FILE} ${FreeRTOS_SRCS} ${SRCS})

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMENT "Building binary file and hex file..."
COMMAND ${CMAKE_OBJCOPY} -Obinary ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin
COMMAND ${CMAKE_OBJCOPY} -Oihex ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex
)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex
COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
COMMENT "Invoking: Cross ARM GNU Print Size"
)


